//
//  MusicDataTool.m
//  Gegeda_music_MY
//
//  Created by lanou3g on 15/11/11.
//  Copyright © 2015年 周莹. All rights reserved.
//
#import "MusicDataTool.h"
#import "SerchModel.h"
#import "LyricModel.h"
#define kUrl @"http://project.lanou3g.com/teacher/UIAPI/MusicInfoList.plist"

//用来记录正在播放的音乐
SerchModel *_playingMusicModel;
//存放所有的音乐
static NSArray *_musicArray;
//存放当前音乐的歌词
static NSMutableArray *_currentLycriArray;
@implementation MusicDataTool

    //获取所有音乐
+(NSArray *)allMusic{
    if (!_musicArray) {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSURL *url = [NSURL  URLWithString:kUrl];
            NSArray  *array = [NSArray  arrayWithContentsOfURL:url];
            NSMutableArray  *mutableArray = [NSMutableArray  array ];
            for (NSDictionary  *dic in array) {
                SerchModel *model = [[SerchModel  alloc] init];
                [model  setValuesForKeysWithDictionary:dic];
                [mutableArray addObject:model];
                
            }
            //使得musicArray 与mutableArray 一样
            _musicArray = [NSArray  arrayWithArray:mutableArray];
            
            dispatch_async(dispatch_get_main_queue(), ^{
               [ [NSNotificationCenter   defaultCenter]postNotificationName:@"musicListDidPrepared" object:nil];
            });
          
        });
    }
    return _musicArray;
}

//获取正在当前播放的音乐
+(SerchModel *)currentPlayingMusicModel{
    return _playingMusicModel;
}

//获取下一首音乐
+(SerchModel *)nestMusicModel{
//获取当前音乐的播放的索引
    NSInteger index = [[self  allMusic]indexOfObject:_playingMusicModel];
    
    //计算下一首的索引
    NSInteger nstIndex = index +1;
    
    //越界处理--如果是最后一首,这播放第一首
    if (nstIndex >= [self  allMusic].count) {
        nstIndex = 0;
    }
       return [self  allMusic][nstIndex];
  }

//获取上一首音乐
+(SerchModel *)previouesMusicModel{
//获取当前播放音乐的索引
    NSInteger  index = [[self allMusic]indexOfObject:_playingMusicModel];
    
    //计算上一首的索引
    
    NSInteger  preindex = index-1;
    //越界处理--如果是第一,这播放最后一首
    
    if (preindex  <0) {
        preindex = [self  allMusic].count-1;
    }
    return [self allMusic][preindex];
    }


//设置当前播放的音乐
+(void)setCurrentPlayingMusicWithModel:(SerchModel *)model{

    if (model  && [[self  allMusic]containsObject:model]) {
        if (_playingMusicModel) {
            //前面的歌曲设置为不再正在播放
            _playingMusicModel.isPlaying = noPlaying;
        }
        
        _playingMusicModel = model;
        //获取当前播放音乐的索引
        NSInteger index  = [[self  allMusic]indexOfObject:model];
        model.isPlaying = playing;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"musicIsPlaying"object:[NSNumber numberWithInteger:index]] ;;
        
    }



}


+(NSMutableArray *)currentLycriArray{
    if (!_currentLycriArray) {
        _currentLycriArray = [NSMutableArray  array];
    }

    return _currentLycriArray;
}


//获取当前音乐歌词
+(NSArray *)currentLyricOfMusic{
//先移除所有歌词
    [[self  currentLycriArray] removeAllObjects];
    //将字符串切割成数组
    NSArray  *array = [_playingMusicModel.lyric  componentsSeparatedByString:@"\n"];
    NSLog(@"%@",array);
    for (NSString *str in array) {
        NSArray  *arr = [str  componentsSeparatedByString:@"]"];
        NSLog(@"%@",arr);
        
        NSString *lyric = arr.lastObject;
        if (lyric.length == 0) {
            continue;
        }
        NSString  *time = [(NSString *)arr.firstObject  substringFromIndex:1];
        NSLog(@"%@",time);
        NSArray  *timeArr = [time  componentsSeparatedByString:@":"];
        
        float  floatTime = [(NSString *)timeArr[0]floatValue]*60 +[(NSString *)timeArr[timeArr.count-1]floatValue];
        
        int intTime = (int)floatTime;
        LyricModel *model = [[LyricModel alloc]initWithTime:intTime WithLyric:lyric];
        [[self currentLycriArray] addObject:model];
        
            }
       return [self  currentLycriArray];
}




@end
